package com.example.demo.service.impl;

import com.example.demo.entity.Tag;
import com.example.demo.exception.BusinessException;
import com.example.demo.mapper.TagMapper;
import com.example.demo.service.TagService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 标签服务实现类
 */
@Service
public class TagServiceImpl implements TagService {
    
    private static final Logger log = LoggerFactory.getLogger(TagServiceImpl.class);
    
    private final TagMapper tagMapper;
    
    public TagServiceImpl(TagMapper tagMapper) {
        this.tagMapper = tagMapper;
    }
    
    @Override
    public Tag getTagById(Long id) {
        return tagMapper.selectById(id);
    }
    
    @Override
    public Tag getTagByName(String name) {
        return tagMapper.selectByName(name);
    }
    
    @Override
    public List<Tag> getAllTags() {
        return tagMapper.selectAllTags();
    }
    
    @Override
    public List<Tag> getPopularTags(int limit) {
        return tagMapper.selectPopularTags(limit);
    }
    
    @Override
    public List<Tag> getOrCreateTags(List<String> tagNames) {
        List<Tag> tags = new ArrayList<>();
        
        for (String tagName : tagNames) {
            if (!StringUtils.hasText(tagName)) {
                continue;
            }
            
            Tag tag = tagMapper.selectByName(tagName.trim());
            if (tag == null) {
                // 创建新标签
                tag = new Tag();
                tag.setName(tagName.trim());
                tag.setColor("#007bff"); // 默认颜色
                tag.setArticleCount(0);
                tag.setCreateTime(LocalDateTime.now());
                
                int rows = tagMapper.insert(tag);
                if (rows == 1) {
                    log.info("创建新标签：{}", tagName);
                }
            }
            
            tags.add(tag);
        }
        
        return tags;
    }
    
    @Override
    public Tag createTag(Tag tag) {
        if (!StringUtils.hasText(tag.getName())) {
            throw new BusinessException("标签名称不能为空");
        }
        
        tag.setCreateTime(LocalDateTime.now());
        if (tag.getArticleCount() == null) {
            tag.setArticleCount(0);
        }
        
        int rows = tagMapper.insert(tag);
        if (rows != 1) {
            throw new BusinessException("创建标签失败");
        }
        
        return tag;
    }
    
    @Override
    public Tag updateTag(Tag tag) {
        if (tag.getId() == null) {
            throw new BusinessException("标签ID不能为空");
        }
        
        int rows = tagMapper.update(tag);
        if (rows != 1) {
            throw new BusinessException("更新标签失败");
        }
        
        return tagMapper.selectById(tag.getId());
    }
    
    @Override
    public boolean deleteTag(Long id) {
        return tagMapper.deleteById(id) == 1;
    }
    
    @Override
    public void incrementArticleCount(Long tagId, int increment) {
        tagMapper.updateArticleCount(tagId, increment);
    }
    
    @Override
    public long getTotalTagCount() {
        return tagMapper.selectTotalCount();
    }
}
